﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Cms.Model;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;

namespace Portal.Cms.Bussiness
{
    public class DocYearRangeProcessBll
    {
        public static DocYearRangeCls CreateDocYearRangeModel(string SiteId, string DocYearRangeId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "    select * from TableDocYearRange " +
                                               "    where DocYearRangeId=@DocYearRangeId order by SortIndex",
                    new DbParam[] 
                    { 
                        new DbParam("DocYearRangeId", DocYearRangeId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            DocYearRangeCls ODocYearRange = DocYearRangeParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return ODocYearRange;
        }



        public static DocYearRangeCls[] ReadingDocYearRange(string SiteId, string GroupDataId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);

            string Query = "select * from TableDocYearRange where frkGroupDataId=@frkGroupDataId order by SortIndex";
            DataSet dsResult =                
                     DBService.GetDataSet(null,
                     Query,
                     new DbParam[] 
                    { 
                        new DbParam("frkGroupDataId", GroupDataId),                    
                    });

            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new DocYearRangeCls[0];
            }
            DocYearRangeCls[] DocYearRange = DocYearRangeParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return DocYearRange;
        }


        public static void AddDocYearRange(
            string SiteId, 
            string GroupDataId, 
            string DocYearRangeName, 
            string FromYear,
            string ToYear,
            string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DocYearRangeId = System.Guid.NewGuid().ToString();
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Insert(null, "TableDocYearRange",
                    new DbParam[] 
                    { 
                        new DbParam("DocYearRangeId", DocYearRangeId),
                        new DbParam("frkGroupDataId", GroupDataId),
                        new DbParam("DocYearRangeName", DocYearRangeName),
                        new DbParam("SortIndex", SortIndex),
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("DataLanguage", DataLanguage),
                        new DbParam("FromYear", FromYear),
                        new DbParam("ToYear", ToYear),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Thêm mới khoảng thời gian văn bản " + DocYearRangeName, 
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName, 
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void SaveDocYearRange(string SiteId, string DocYearRangeId, string DocYearRangeName, string FromYear,string ToYear, string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.Update(null, "TableDocYearRange", "DocYearRangeId", DocYearRangeId,
                    new DbParam[] 
                    { 
                        new DbParam("DocYearRangeName", DocYearRangeName),
                        new DbParam("FromYear", FromYear),
                        new DbParam("ToYear", ToYear),
                        new DbParam("SortIndex", SortIndex),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Sửa chữa khoảng thời gian văn bản " + DocYearRangeName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName,
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void DeleteDocYearRange(string SiteId, string DocYearRangeId)
        {
            DocYearRangeCls ODocYearRange = DocYearRangeProcessBll.CreateDocYearRangeModel(SiteId, DocYearRangeId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableDocYearRange where DocYearRangeId=@DocYearRangeId; ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("DocYearRangeId", DocYearRangeId),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Xóa khoảng thời gian văn bản " + ODocYearRange.DocYearRangeName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName,
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }
    }
}
